var active = {
    //初始化折线图
    initEchart: function (type) {
        var dates = [];
        var values = [];
        var title = "";
        var subtext = "";
        $.ajax({
            url: "/interface/getPriceTrendList",
            data: {},
            dataType: "json",
            type: "post",
            async: false,
            success: function (res) {
                if (res.returnCode === "00") {
                    if (type == "blt") {
                        dates = res.data.bltDates;
                        values = res.data.bltvalues;
                        title = "布伦特原油指数价格走势图";
                    } else if (type == "ld") {
                        dates = res.data.ldDates;
                        values = res.data.ldvalues;
                        title = "伦敦原油指数价格走势图";
                    }
                    subtext = "来源：中宇咨询   日期：" + active.toDateString(new Date());
                    // $(".zstu-title").html(title);
                } else if (res.returnCode === "01") {
                    console.log(res);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.error(XMLHttpRequest.status);
                console.error(XMLHttpRequest.readyState);
                console.error(textStatus);
            }
        });
        // 基于准备好的dom，初始化echarts实例
        var chart = echarts.init(document.getElementById('chart'));
        var option = {
            title: {
                text: title,
                subtext: subtext,
                textStyle: {
                    //文字颜色
                    color: '#555555',
                    //字体风格,'normal','italic','oblique'
                    fontStyle: 'normal',
                    //字体粗细 'normal','bold','bolder','lighter',100 | 200 | 300 | 400...
                    fontWeight: 'normal',
                    //字体大小
                    fontSize: 20
                }
            },
            tooltip: {
                trigger: 'axis'
            },
            toolbox: {
                feature: {
                    saveAsImage: {}
                }
            },
            grid: {
                top: '20%',
                left: '10%',
                right: '10%',
                bottom: '15%',
                containLabel: true
            },
            xAxis: [
                {
                    type: 'category',
                    boundaryGap: false,
                    axisLabel: {
                        "interval": 0,
                        "rotate": 70,
                        "show": true,
                        "splitNumber": 15,
                        "textStyle": {
                            "fontFamily": "微软雅黑",
                            "fontSize": 12
                        }
                    },
                    data: dates.map(function (str) {
                        return str.replace(' ', '\n')
                    })
                }
            ],
            yAxis: [
                {
                    type: 'value',
                    name: '(美元/吨)'
                }
            ],
            series: [
                {
                    name: '价格',
                    type: 'line',
                    hoverAnimation: false,
                    itemStyle: {
                        normal: {
                            color: '#ea5715',
                            lineStyle: {
                                color: '#ea5715'
                            }
                        }
                    },
                    data: values
                }
            ]
        };
        // 使用刚指定的配置项和数据显示图表。
        chart.setOption(option);
    },
    //时间戳的处理
    toDateString: function (d, format) {
        var date = new Date(d || new Date())
            , ymd = [
            active.digit(date.getFullYear(), 4)
            , active.digit(date.getMonth() + 1)
            , active.digit(date.getDate())
        ]
            , hms = [
            active.digit(date.getHours())
            , active.digit(date.getMinutes())
            , active.digit(date.getSeconds())
        ];

        format = format || 'yyyy-MM-dd HH:mm:ss';

        return format.replace(/yyyy/g, ymd[0])
            .replace(/MM/g, ymd[1])
            .replace(/dd/g, ymd[2])
            .replace(/HH/g, hms[0])
            .replace(/mm/g, hms[1])
            .replace(/ss/g, hms[2]);
    },
    //数字前置补零
    digit: function (num, length, end) {
        var str = '';
        num = String(num);
        length = length || 2;
        for (var i = num.length; i < length; i++) {
            str += '0';
        }
        return num < Math.pow(10, length) ? str + (num | 0) : num;
    }
};

$(function () {
    var type = QueryUtils.GetQueryString("type");
    active.initEchart(type);
});
